package com.flejay.dreamrecruiter.service.impl;

import java.text.Normalizer;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import javax.inject.Named;
import javax.inject.Singleton;

import com.flejay.dreamrecruiter.service.ICleanerService;

@Singleton
@Named("cleanerService")
public class CleanerService implements ICleanerService{

	private final String replacementRegex = "\n\t\t$1\n\t\t";
	
	@Override
	public List<String> prepareLine(final String line) {
		List<String> resList = new ArrayList<String>();
		String res = line;
		// letters
		res = res.replaceAll("ﬁ", "fi");
		res = res.replaceAll("ﬂ", "fl");
		res = res.replaceAll("œ", "oe");
		res = res.replaceAll("Œ", "oe");
		res = res.replaceAll("æ", "ae");
		res = res.replaceAll("Æ", "AE");
		// strange characters
		res = res.replaceAll("…", "...");
		res = res.replaceAll("’", "'");
		res = res.replaceAll("”", "\"");
		res = res.replaceAll("“", "\"");
		res = res.replaceAll("„", "\"");
		res = res.replaceAll("--+", "");
		res = res.replaceAll("__+", "");
		res = res.replaceAll("\\. ?\\. ?\\. ?\\. ?(\\. ?)*]", " ");
		res = res.replaceAll("●", "");
		res = res.replaceAll("○", "");
		res = res.replaceAll("", "");
		res = res.replaceAll("☏", "");
		res = res.replaceAll("!", "");
		res = res.replaceAll("\u0001", "");
		res = res.replaceAll("\u0002", "");
		res = res.replaceAll("\u0003", "");
		// spaces
		res = res.replaceAll("(\\s)+(\\s)+", "\n");
		res = res.replaceAll("|||", "");
		res = res.replaceAll("\\s+", " ");
		res = res.replaceAll("^\\s", "");
		res = res.replaceAll("\\s$", "");
		// useless symbols
		res = res.replaceAll("||||⊕|†", "");
		res = res.replaceAll("^ ?$", "");
		res = res.replaceAll("-­‐", "-");
		// normalize
		res = Normalizer.normalize(res, Normalizer.Form.NFD).replaceAll("[^\\p{ASCII}]", "");
		// replace ' - '
		res = res.replaceAll("\\s-\\s", "\n");
		res = res.replaceAll(" - ", "\n");
		res = res.replaceAll("\\s-\\s", "\n");
		// remove lasting non AlphaNum+
		res = res.replaceAll("\\*", "");
		res = res.replaceAll("^\\#", "");
		// add \n to delimit email, number ...
		res = res.replaceAll("([a-z0-9_%+-\\.]+@[a-z0-9.-]+\\.[a-z]{2,4})", replacementRegex);
		res = res.replaceAll("([0-9]{10})", "\n\t\t$1\n\t\t");
		res = res.replaceAll("((\\()?[0-9]{3}(\\))?(\\s[0-9]{3})(\\s[0-9]{4}))", replacementRegex);
		res = res.replaceAll("((\\()?[0-9]{3}(\\))?(\\s[0-9]{3})(\\-[0-9]{4}))", replacementRegex);
		res = res.replaceAll("((\\()?[0-9]{3}(\\))?(\\-[0-9]{3})(\\-[0-9]{4}))", replacementRegex);
		res = res.replaceAll("((\\()?[0-9]{3}(\\))?(\\-[0-9]{3})(\\s[0-9]{4}))", replacementRegex);
		res = res.replaceAll("(00 33 [1-6]{1}((([0-9]{2}){4})|(((\\s)[0-9]{2}){4})|((-[0-9]{2}){4})|((\\.[0-9]{2}){4})))", replacementRegex);
		// # ANT: delimit address
		res = res.replaceAll("(\\d+, ([a-z]+ (.*)))", replacementRegex);
		res = res.replaceAll("(\\s)+$", "");
		res = res.replaceAll("^(\\s)+", "");
		//Quand il y a 3 espaces d'affilé on sépare en plusieurs lignes
		Pattern p = Pattern.compile("(.*)(   )(.*)");
		if (p.matcher(res).find()){
			for (String explosedLines : res.split("((\\s)+(\\s)+(\\s)+)")){
				resList.add(explosedLines);
			}
		} else {
			resList.add(res);
		}
		return resList;
	}
}
